//
//  SettViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/5/29.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "SettViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "SeetTableViewCell.h"
#import "Alart_ZhiFuView.h"
#import "YoungAllData.h"
#import "GMDCircleLoader.h"
#import "LogisticsListInFoViewController.h"
@interface SettViewController ()<UITableViewDataSource,UITableViewDelegate,YoungDelegate,UITextFieldDelegate>
{
    UIView *backView;
    UITableView *zTableView;
    Alart_ZhiFuView *_zhiFuAlartView;//提交订单时的弹出框
    UIView *_alertBackview;
    UIView *views;
    UIView *alertView;
}
@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation SettViewController
@synthesize zArrayData;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden=YES;
    
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"物流费清单";
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];

    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data wuLiuFeiZhangDan:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]];
    
}
#pragma mark - 物流费账单列表回调
-(void)WuLiuZhangDan:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"]isEqualToString:@"success"]) {
        zArrayData = [[NSMutableArray alloc]initWithArray:[[sign objectForKey:@"data"] objectForKey:@"OrderList"]];
        [self initUI:[[sign objectForKey:@"data"] objectForKey:@"ExpressFeeTotal"]];
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - initUI
-(void)initUI:(NSString *)Total{
    backView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, 300)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    UILabel *zNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, SCREEN_WIDTH/2+40, 30)];
    zNameLabel.textColor = [UIColor grayColor];
    zNameLabel.text = @"日期";
    zNameLabel.textAlignment = NSTextAlignmentCenter;
    zNameLabel.font = [UIFont systemFontOfSize:14];
    [backView addSubview:zNameLabel];
    
    
    UILabel *zNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+40,20, SCREEN_WIDTH/2-10-40, 30)];
    zNumberLabel.textColor = [UIColor grayColor];
    zNumberLabel.text = @"物流费";
    zNumberLabel.textAlignment = NSTextAlignmentCenter;
    zNumberLabel.font = [UIFont systemFontOfSize:14];
    [backView addSubview:zNumberLabel];
    
    [self createrOffLineUI:CGRectMake(10, 20, SCREEN_WIDTH-20, 0.5)];
    [self createrOffLineUI:CGRectMake(10, 50, SCREEN_WIDTH-20, 0.5)];
    [self createrOffLineUI:CGRectMake(10, 20, 0.5, 30)];
    [self createrOffLineUI:CGRectMake(SCREEN_WIDTH-10, 20, 0.5, 30)];
    [self createrOffLineUI:CGRectMake(SCREEN_WIDTH/2+40, 20, 0.5, 30)];
    
    
    zTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,51, SCREEN_WIDTH, 120)];
    //zTableView.backgroundColor = [UIColor redColor];
    zTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    zTableView.delegate = self;
    zTableView.dataSource = self;
    zTableView.bounces = NO;
    [backView addSubview:zTableView];
    
    UILabel *zTotal = [[UILabel alloc]initWithFrame:CGRectMake(10,zTableView.bottom+10, 42, 20)];
    zTotal.textColor = [UIColor blackColor];
    zTotal.text = @"共计:";
    zTotal.textAlignment = NSTextAlignmentLeft;
    zTotal.font = [UIFont systemFontOfSize:14];
    [backView addSubview:zTotal];

    UILabel *zTotalValue = [[UILabel alloc]initWithFrame:CGRectMake(zTotal.right,zTableView.bottom+10, 160, 20)];
    zTotalValue.textColor = [self colorWithHexString:@"#EC3472"];
    zTotalValue.text = [NSString stringWithFormat:@"￥%@",Total];
    zTotalValue.textAlignment = NSTextAlignmentLeft;
    zTotalValue.font = [UIFont systemFontOfSize:14];
    [backView addSubview:zTotalValue];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, zTotal.bottom+10, SCREEN_WIDTH-20, 26)];
    imageView.image = [UIImage imageNamed:@"物流费清单_03"];
    [backView addSubview:imageView];
    
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setBackgroundImage:[UIImage imageNamed:@"物流费清单_07"] forState:UIControlStateNormal];
    setBtn.frame = CGRectMake(20, backView.bottom+20, SCREEN_WIDTH-40, 40);
    [setBtn addTarget:self action:@selector(clickSetBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:setBtn];
}
#pragma mark - UitableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [zArrayData count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SeetTableViewCell *cell = [[SeetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    cell.zName.text = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"Duration"];
    cell.zNumber.text = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"TotalAmount"];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LogisticsListInFoViewController *logist = [[LogisticsListInFoViewController alloc]init];
    logist.ExpressBillID = [[zArrayData objectAtIndex:indexPath.row]objectForKey:@"ExpressBillID"];
    logist.time = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"Duration"];
    logist.wuliuPrace = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"TotalAmount"];
    [self.navigationController pushViewController:logist animated:YES];
}

#pragma mark - 结算
-(void)clickSetBtn{
    [self careteAlertPassword];
}
#pragma -mark 提交订单时的弹出输入密码提示框
-(void)careteAlertPassword{
    
    UIWindow *windows = [[[UIApplication sharedApplication] delegate] window];
    
    _alertBackview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _alertBackview.backgroundColor = [UIColor blackColor];
    _alertBackview.alpha = 0.4;
    [windows addSubview:_alertBackview];
    
    _zhiFuAlartView = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_Cart_Xib" owner:self options:nil] objectAtIndexedSubscript:12];
    _zhiFuAlartView.frame = CGRectMake(60, SCREEN_HEIGHT/2-90, SCREEN_WIDTH-120, 140);
    _zhiFuAlartView.password.delegate = self;
    [_zhiFuAlartView.affirmBut addTarget:self action:@selector(affirmClick) forControlEvents:UIControlEventTouchUpInside];
    [_zhiFuAlartView.cancelBut addTarget:self action:@selector(cancelButClick) forControlEvents:UIControlEventTouchUpInside];
    [windows addSubview:_zhiFuAlartView];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [_alertBackview addGestureRecognizer:singleTapGestureRecognizer];

    
    NSString *title = [NSString stringWithFormat:@"请输入您的支付密码"];
    _zhiFuAlartView.titleAlart.text = title;
}

-(void)singleTap{
    [_zhiFuAlartView.password resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
//    NSMutableString *text = [textField.text mutableCopy];
//    [text replaceCharactersInRange:range withString:string];
//    return ([text length] >= 6);
    return YES;
}

- (void)requistFail{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"网络错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma -mark 点击弹出框的确定交易
- (void)affirmClick{
    if (_zhiFuAlartView.password.text== nil ||[_zhiFuAlartView.password.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入支付密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    [_alertBackview removeFromSuperview];
    [_zhiFuAlartView removeFromSuperview];
    [self jiesuan];
}

#pragma -mark 结算请求服务器
-(void)jiesuan{
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data wuLiuFeiJie:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andTradePass:_zhiFuAlartView.password.text];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
}
#pragma mark - 结算全部物流费回调
-(void)WuLiuPay:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"]isEqualToString:@"success"]) {
        [self jiesuanSuccess];
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 结算完成
-(void)jiesuanSuccess{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    views = [[UIView alloc]init];
    views.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT+100);
    views.userInteractionEnabled = YES;
    views.backgroundColor = [UIColor blackColor];
    views.alpha = 0.4;
    [window addSubview:views];
    
    alertView = [[UIView alloc]initWithFrame:CGRectMake(50, SCREEN_HEIGHT/2-150, SCREEN_WIDTH-100, 140)];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.userInteractionEnabled = YES;
    alertView.layer.cornerRadius = 10;//设置那个圆角的有多圆
    [window addSubview:alertView];
    
    
    UIImageView *leftRightImage = [[UIImageView alloc]initWithFrame:CGRectMake(70, 45, 20, 22)];
    leftRightImage.image = [UIImage imageNamed:@"dingdanchong"];
    [alertView addSubview:leftRightImage];
    
    
    //验证码输入错误
    UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftRightImage.right, leftRightImage.top, 100, 22)];
    alertLabel.text = @"结算完成";
    alertLabel.numberOfLines = 0;
    alertLabel.font = [UIFont systemFontOfSize:14];
    alertLabel.textAlignment = NSTextAlignmentLeft;
    [alertView addSubview:alertLabel];
    
    UIView *lines = [[UIView alloc] initWithFrame:CGRectMake(0, 140-31, alertView.frame.size.width, 0.5)];
    lines.backgroundColor = [UIColor grayColor];
    [alertView addSubview:lines];
    
    
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, alertLabel.bottom+50-3, alertView.frame.size.width, 31);
    //leftBtn.backgroundColor= [ UIColor whiteColor];
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:leftBtn.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = leftBtn.bounds;
    maskLayer1.path = maskPath1.CGPath;
    leftBtn.layer.mask = maskLayer1;
    [leftBtn addTarget:self action:@selector(clickTongGuo) forControlEvents:UIControlEventTouchUpInside];
    
    [leftBtn setImage:[UIImage imageNamed:@"weixindengluhou-07"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"weixindengluhou-07"] forState:UIControlStateHighlighted];
    [alertView addSubview:leftBtn];
    
}
#pragma mark - 结算完成
-(void)clickTongGuo{
    [views removeFromSuperview];
    [alertView removeFromSuperview];
}

#pragma -mark 点击弹出框的取消交易
- (void)cancelButClick{
    [_alertBackview removeFromSuperview];
    [_zhiFuAlartView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createrOffLineUI:(CGRect)rect{
    UIView *lines = [[UIView alloc] initWithFrame:rect];
    lines.backgroundColor = [UIColor colorWithRed:179/255.0 green:179/255.0 blue:179/255.0 alpha:1];
    [backView addSubview:lines];
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
